package com.pfizer.pfizerExpertForPad;

import java.io.File;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.text.Editable;
import android.text.Selection;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.View.OnKeyListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

import com.pfizer.DB.DataHelper;
import com.pfizer.Entity.Handout;
import com.pfizer.Entity.Question;
import com.pfizer.Entity.SurveyResult;
import com.pfizer.Entity.pVideo;
import com.pfizer.PicView.OnEndSnapListener;
import com.pfizer.PicView.ScrollLayout;
import com.pfizer.Utils.HandoutListAdapter;
import com.pfizer.Utils.OnAnswerSelectedListener;
import com.pfizer.Utils.QuestionListAdapter;
import com.pfizer.Utils.SurveyListAdapter;
import com.pfizer.VideoPlayer.VideoView;

/**
 * DiseaseActivity--<b>主系統功能區<b>-- 該類展示了文章、視頻、問卷調查、DAS計算功能
 */
public class DiseaseActivity extends Activity implements OnClickListener,
		OnLongClickListener, OnItemClickListener, OnKeyListener,
		OnFocusChangeListener {
	// private static final String TAG = "DiseaseAcitvieySample";
	// private final static float TARGET_HEAP_UTILIZATION = 0.75f;
	// private final static int CWJ_HEAP_SIZE = 40* 1024* 1024 ;
	// 全局變量設置---begin--
	Context mContext;
	DataHelper dHelper;
	pfizerApplication pApplication;
	String ClassID = "";
	InputMethodManager m = null;
	RelativeLayout[] rl = new RelativeLayout[5];
	private int[] resTitle = new int[] { R.drawable.disease_titlebar1,
			R.drawable.disease_titlebar2, R.drawable.disease_titlebar3,
			R.drawable.disease_titlebar4 };
	private int[] resBottom = new int[] { R.drawable.disease_bottombar1,
			R.drawable.disease_bottombar2, R.drawable.disease_bottombar3,
			R.drawable.disease_bottombar4 };
	private int[] resVideoDesp = new int[] { R.drawable.bgdesp01,
			R.drawable.bgdesp02, R.drawable.bgdesp03, R.drawable.bgdesp04 };
	private int iCurrRl = 1;
	private Button[] btn = new Button[5];

	private Button btnInputPreCode = null;
	private Animation animationTranslate;
	private ImageView log = null;
	// 全局變量設置---end--

	// 文章圖片預覽參數設置---begin--
	ScrollLayout mScrollLayout;
	LinearLayout llPicConvert = null;
	RelativeLayout rlHandout = null;
	RelativeLayout rlPicView = null;
	LinearLayout llDasRight = null;
	boolean isHandoutFullScreen = false;
	int imageCount = 0;
	TextView tv = null;
	private Button btnTest = null;
	ListView lvHandout;
	List<Map<String, Object>> lmHandout = null;
	HandoutListAdapter laHandout = null;
	private Button btnPage = null;
	private final Timer timer = new Timer();
	private TimerTask task;
	Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			btnPage.setVisibility(View.GONE);
			super.handleMessage(msg);
		}
	};
	// 文章圖片預覽參數設置---end

	// 影片播放參數設置---begin--
	private static int screenWidth = 0;
	private static int screenHeight = 0;
	private final static int SCREEN_FULL = 0;
	private final static int SCREEN_DEFAULT = 1;

	// 衛教影片模塊參數設置--begin--
	List<Map<String, Object>> lmHVideo = null;
	HandoutListAdapter laHVideo = null;
	VideoView vhVideo;
	ImageButton btnhControll;
	ImageButton btnhScreen;
	TextView tvhVTime;
	TextView tvhVTitle;
	TextView tvhVDesp;
	ListView lvhVideo;
	RelativeLayout rlhVideoView = null;
	RelativeLayout llhController = null;
	private boolean ishOnline = false;
	private boolean ishPaused = false;
	private boolean ishVideoFullScreen = false;
	private String shEndTime = "00:00:00";
	private final static int HPROGRESS_CHANGED = 10;
	Handler myhHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			switch (msg.what) {
			case HPROGRESS_CHANGED:
				int i = vhVideo.getCurrentPosition();
				i /= 1000;
				int minute = i / 60;
				int hour = minute / 60;
				int second = i % 60;
				minute %= 60;
				tvhVTime.setText(String.format("%02d:%02d:%02d/%s", hour,
						minute, second, shEndTime));
				sendEmptyMessageDelayed(HPROGRESS_CHANGED, 100);
				break;
			}
			super.handleMessage(msg);
		}
	};
	// 衛教影片模塊參數設置--end--

	// 見證影片模塊參數設置--begin--
	VideoView vwVideo;
	ImageButton btnwControll;
	ImageButton btnwScreen;
	TextView tvwVTime;
	TextView tvwVTitle;
	TextView tvwVDesp;
	ListView lvwVideo;
	RelativeLayout rlwVideoView = null;
	RelativeLayout llwController = null;
	List<Map<String, Object>> lmWVideo;
	HandoutListAdapter laWVideo;
	private boolean iswOnline = false;
	private boolean iswPaused = false;
	private boolean iswVideoFullScreen = false;
	private String swEndTime = "00:00:00";
	private final static int WPROGRESS_CHANGED = 9;
	Handler mywHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			switch (msg.what) {
			case WPROGRESS_CHANGED:
				int i = vwVideo.getCurrentPosition();
				i /= 1000;
				int minute = i / 60;
				int hour = minute / 60;
				int second = i % 60;
				minute %= 60;
				tvwVTime.setText(String.format("%02d:%02d:%02d/%s", hour,
						minute, second, swEndTime));
				sendEmptyMessageDelayed(WPROGRESS_CHANGED, 100);
				break;
			}
			super.handleMessage(msg);
		}
	};
	// 見證影片模塊參數設置--end--
	// 影片播放參數設置---end--

	// 問卷模塊參數設置--begin--
	EditText etSubject = null;
	EditText etHospital = null;
	EditText etDoctorName = null;
	TextView tvAnswered = null;
	ListView lvSurvey = null;
	ListView lvQuestions = null;
	RelativeLayout rlqSurvey = null;
	Button btnSurveySend = null;
	List<Map<String, Object>> lmSurvey;
	SurveyListAdapter laSurvey;
	List<Question> questionsList;
	QuestionListAdapter laQuestion;
	// 問卷模塊參數設置--end--

	// DAS計算參數設置---begin--
	private TextView showDAS28 = null;
	private TextView showActivity = null;
	private TextView tvMgMm = null;
	private Button[] btnDAScalc = new Button[5];
	private EditText[] et = new EditText[4];
	String strDAS28Val = "";
	String strActivity = "";
	double TJC28Val = 0.0;
	double CPRMgdlVal = 0.0;
	double SJC28Val = 0.0;
	double VASVal = 0.0;
	double DAS28Val = 0.0;
	boolean isCPR = true;
	boolean isESR = false;
	boolean isBtnPointClicked = false;

	// DAS計算參數設置---end--

	/**
	 * onCreate:創建系統截面
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		// VMRuntime.getRuntime().setMinimumHeapSize(CWJ_HEAP_SIZE);
		// VMRuntime.getRuntime().setTargetHeapUtilization(TARGET_HEAP_UTILIZATION);

		setContentView(R.layout.diseaselayout);
		m = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
		// 初始化全局--begin--
		mContext = DiseaseActivity.this;
		InitApp initApp = new InitApp(mContext);
		initApp.SetApp(0);
		pApplication = (pfizerApplication) getApplicationContext();
		dHelper = pApplication.getDataHelper();
		Intent intent = this.getIntent();
		ClassID = intent.getExtras().getString("ClassID");
		int iRecID = intent.getExtras().getInt("RecID");
		if (ClassID == null || ClassID == "") {
			ClassID = "1";
		}
		int iClassId = Integer.valueOf(ClassID);
		View vTitle = this.findViewById(R.id.llTop);
		vTitle.setBackgroundResource(resTitle[iClassId - 1]);
		View vBottom = this.findViewById(R.id.rlBottom);
		vBottom.setBackgroundResource(resBottom[iClassId - 1]);
		// 初始化顯示截面
		rl[0] = (RelativeLayout) this.findViewById(R.id.rlhVideo);
		rl[1] = (RelativeLayout) this.findViewById(R.id.rlHandout);
		rl[2] = (RelativeLayout) this.findViewById(R.id.rlDas);
		rl[3] = (RelativeLayout) this.findViewById(R.id.rlwVideo);
		rl[4] = (RelativeLayout) this.findViewById(R.id.rlSurvey);
		// 初始化按鈕
		btn[0] = (Button) this.findViewById(R.id.btnHealthEducateVideo);
		btn[0].setOnClickListener(this);
		btn[1] = (Button) this.findViewById(R.id.btnHandout);
		btn[1].setOnClickListener(this);
		btn[2] = (Button) this.findViewById(R.id.btnDas);
		btn[2].setOnClickListener(this);
		btn[3] = (Button) this.findViewById(R.id.btnWitnessVideo);
		btn[3].setOnClickListener(this);
		btn[4] = (Button) this.findViewById(R.id.btnSurvey);
		btn[4].setOnClickListener(this);

		btnInputPreCode = (Button) this.findViewById(R.id.btnInputPreCode);
		btnInputPreCode.setOnClickListener(this);
		log = (ImageView) this.findViewById(R.id.imgDiseaseLog);
		log.setOnClickListener(this);
		// 初始化全局变量-end--

		// 初始化影片播放截面
		View v1 = this.findViewById(R.id.rlhVideoDesp);
		v1.setBackgroundResource(resVideoDesp[iClassId - 1]);
		View v2 = this.findViewById(R.id.rlwVideoDesp);
		v2.setBackgroundResource(resVideoDesp[iClassId - 1]);

		llhController = (RelativeLayout) this.findViewById(R.id.llhController);
		btnhControll = (ImageButton) this.findViewById(R.id.btnhControll);
		vhVideo = (VideoView) this.findViewById(R.id.vvhMovies);

		llwController = (RelativeLayout) this.findViewById(R.id.llwController);
		btnwControll = (ImageButton) this.findViewById(R.id.btnwControll);
		vwVideo = (VideoView) this.findViewById(R.id.vvwMovies);

		// 获取文章相关view引用--begin--
		mScrollLayout = (ScrollLayout) this.findViewById(R.id.slPicview);
		llPicConvert = (LinearLayout) this.findViewById(R.id.llPicConvert);
		rlPicView = (RelativeLayout) this.findViewById(R.id.rlPicView);
		btnTest = (Button) this.findViewById(R.id.btnTest);
		btnPage = (Button) this.findViewById(R.id.btnPage);
		lvHandout = (ListView) findViewById(R.id.lvHandout);
		lvHandout.setDividerHeight(0);
		mScrollLayout.setOnEndSnapListener(new OnEndSnapListener() {

			@Override
			public void onEndSnap(int currScreen) {
				// TODO Auto-generated method stub
				setHandoutButtonBackGround(currScreen + 1);
				if (isHandoutFullScreen) {
					btnPage.setText(String.format("第%d頁", currScreen + 1));
					btnPage.setVisibility(View.VISIBLE);
					task = new TimerTask() {
						@Override
						public void run() {
							// TODO Auto-generated method stub
							Message message = new Message();
							message.what = 1;
							handler.sendMessage(message);
						}
					};
					timer.schedule(task, 3000);
				}
			}
		});

		// 获取文章相关view引用--end--

		// getScreenSize();
		// 获取问卷相关view引用--end--
		// 初始化显示内容

		switch (iRecID) {
		case 0:
			setHVideoList(1);
			break;
		case 1:
			setHandoutList();
			break;
		case 2:
			initDASLayout();
			break;
		case 3:
			setWVideoList(2);
			break;
		case 4:
			initSurveyLayout();
			setSurveyList();
			etDoctorName.requestFocus();
			break;
		}
		DisplayFuncRect(iRecID);
	}

	private void initSurveyLayout() {
		if (lvSurvey == null) {
			// 获取问卷相关view引用--begin--
			etSubject = (EditText) this.findViewById(R.id.etSubject);
			etSubject.setOnKeyListener(this);
			etSubject.setOnFocusChangeListener(this);
			etHospital = (EditText) this.findViewById(R.id.etHospital);
			etHospital.setOnKeyListener(this);
			etHospital.setOnFocusChangeListener(this);
			etDoctorName = (EditText) this.findViewById(R.id.etDoctorName);
			etDoctorName.setOnKeyListener(this);
			etDoctorName.setOnFocusChangeListener(this);
			tvAnswered = (TextView) this.findViewById(R.id.tvAnswered);
			lvSurvey = (ListView) this.findViewById(R.id.lvSurvey);
			rlqSurvey = (RelativeLayout) this.findViewById(R.id.rlqSurvey);

			lvQuestions = (ListView) this.findViewById(R.id.lvQuestions);
			btnSurveySend = (Button) this.findViewById(R.id.btnSurveySend);
			btnSurveySend.setOnClickListener(this);
			lvSurvey.setDividerHeight(0);
			lvSurvey.setOnItemClickListener(this);
			lvQuestions.setDividerHeight(0);
		}
	}

	private void initDASLayout() {
		if (llDasRight == null) {
			// 获取DAS相关view引用
			et[0] = (EditText) this.findViewById(R.id.etTenderJoints);
			et[0].setId(1001);
			et[0].setOnClickListener(this);
			et[0].setOnKeyListener(this);
			et[0].setOnFocusChangeListener(this);
			et[1] = (EditText) this.findViewById(R.id.etMgdl);
			et[1].setId(1002);
			et[1].setOnClickListener(this);
			et[1].setOnKeyListener(this);
			et[1].setOnFocusChangeListener(this);
			et[2] = (EditText) this.findViewById(R.id.etSwollenJoints);
			et[2].setId(1003);
			et[2].setOnClickListener(this);
			et[2].setOnKeyListener(this);
			et[2].setOnFocusChangeListener(this);
			et[3] = (EditText) this.findViewById(R.id.etVAS);
			et[3].setId(1004);
			et[3].setOnClickListener(this);
			et[3].setOnKeyListener(this);
			et[3].setOnFocusChangeListener(this);
			btnDAScalc[0] = (Button) this.findViewById(R.id.btnCPR);
			btnDAScalc[0].setOnClickListener(this);
			btnDAScalc[1] = (Button) this.findViewById(R.id.btnESR);
			btnDAScalc[1].setOnClickListener(this);
			btnDAScalc[2] = (Button) this.findViewById(R.id.btnPoint);
			btnDAScalc[2].setOnClickListener(this);
			btnDAScalc[3] = (Button) this.findViewById(R.id.btnRun);
			btnDAScalc[3].setOnClickListener(this);
			btnDAScalc[4] = (Button) this.findViewById(R.id.btnClear);
			btnDAScalc[4].setOnClickListener(this);
			showDAS28 = (TextView) this.findViewById(R.id.tvDAS28);
			showActivity = (TextView) this.findViewById(R.id.tvActivity);
			llDasRight = (LinearLayout) this.findViewById(R.id.llDasRight);
			tvMgMm = (TextView) this.findViewById(R.id.tvMgMm);
		}
	}

	private void initHVideoLayout() {
		// 获取卫教影片播放相关view引用--begin--
		if (rlhVideoView == null) {
			rlhVideoView = (RelativeLayout) this
					.findViewById(R.id.rlhVideoView);

			btnhControll.setOnClickListener(this);
			btnhScreen = (ImageButton) this.findViewById(R.id.btnhScreen);
			btnhScreen.setOnClickListener(this);
			tvhVTime = (TextView) this.findViewById(R.id.tvhVTime);
			tvhVTitle = (TextView) this.findViewById(R.id.tvhVTitle);
			tvhVDesp = (TextView) this.findViewById(R.id.tvhVDesp);
			lvhVideo = (ListView) this.findViewById(R.id.lvhVideo);
			lvhVideo.setDividerHeight(0);

			vhVideo.setOnLongClickListener(this);
			vhVideo.setOnErrorListener(new OnErrorListener() {
				public boolean onError(MediaPlayer mp, int what, int extra) {

					vhVideo.stopPlayback();
					ishOnline = false;

					new AlertDialog.Builder(DiseaseActivity.this).setTitle(
							"对不起").setMessage("您所播的视频格式不正确，播放已停止。")
							.setPositiveButton("知道了",
									new AlertDialog.OnClickListener() {

										public void onClick(
												DialogInterface dialog,
												int which) {
											vhVideo.stopPlayback();
										}
									}).setCancelable(false).show();

					return false;
				}

			});

			vhVideo.setOnPreparedListener(new OnPreparedListener() {

				public void onPrepared(MediaPlayer arg0) {
					// TODO Auto-generated method stub

					setHVideoScale(SCREEN_DEFAULT);
					ishVideoFullScreen = false;
					int i = vhVideo.getDuration();
					i /= 1000;
					int minute = i / 60;
					int hour = minute / 60;
					int second = i % 60;
					minute %= 60;
					shEndTime = String.format("%02d:%02d:%02d", hour, minute,
							second);
					// tvhVTime.setText(String.format("00:00:00/%s",shEndTime));
					vhVideo.start();
					btnhControll.setBackgroundResource(R.drawable.btnpause);
					myhHandler.sendEmptyMessage(HPROGRESS_CHANGED);
				}
			});

			vhVideo.setOnCompletionListener(new OnCompletionListener() {

				public void onCompletion(MediaPlayer arg0) {
					// TODO Auto-generated method stub
					vhVideo.stopPlayback();
					btnhControll.setBackgroundResource(R.drawable.btnplay);
				}
			});
		}
		// 获取卫教影片播放相关view引用--end--
	}

	private void initWVideoLayout() {
		// 获取见证影片播放相关view引用--begin--
		if (rlwVideoView == null) {
			rlwVideoView = (RelativeLayout) this
					.findViewById(R.id.rlwVideoView);

			btnwControll.setOnClickListener(this);
			btnwScreen = (ImageButton) this.findViewById(R.id.btnwScreen);
			btnwScreen.setOnClickListener(this);
			tvwVTime = (TextView) this.findViewById(R.id.tvwVTime);
			tvwVTitle = (TextView) this.findViewById(R.id.tvwVTitle);
			tvwVDesp = (TextView) this.findViewById(R.id.tvwVDesp);
			lvwVideo = (ListView) this.findViewById(R.id.lvwVideo);
			lvwVideo.setDividerHeight(0);

			vwVideo.setOnLongClickListener(this);
			vwVideo.setOnErrorListener(new OnErrorListener() {
				public boolean onError(MediaPlayer mp, int what, int extra) {

					vwVideo.stopPlayback();
					iswOnline = false;

					new AlertDialog.Builder(DiseaseActivity.this).setTitle(
							"对不起").setMessage("您所播的视频格式不正确，播放已停止。")
							.setPositiveButton("知道了",
									new AlertDialog.OnClickListener() {

										public void onClick(
												DialogInterface dialog,
												int which) {
											vwVideo.stopPlayback();
										}
									}).setCancelable(false).show();

					return false;
				}

			});

			vwVideo.setOnPreparedListener(new OnPreparedListener() {

				public void onPrepared(MediaPlayer arg0) {
					// TODO Auto-generated method stub

					setWVideoScale(SCREEN_DEFAULT);
					iswVideoFullScreen = false;
					int i = vwVideo.getDuration();
					i /= 1000;
					int minute = i / 60;
					int hour = minute / 60;
					int second = i % 60;
					minute %= 60;
					swEndTime = String.format("%02d:%02d:%02d", hour, minute,
							second);
					tvwVTime.setText(String.format("00:00:00/%s", swEndTime));
					vwVideo.start();
					btnwControll.setBackgroundResource(R.drawable.btnpause);
					mywHandler.sendEmptyMessage(WPROGRESS_CHANGED);
				}
			});

			vwVideo.setOnCompletionListener(new OnCompletionListener() {

				public void onCompletion(MediaPlayer arg0) {
					// TODO Auto-generated method stub
					iswOnline = false;
					vwVideo.stopPlayback();
					btnwControll.setBackgroundResource(R.drawable.btnplay);
				}
			});
			// 获取卫教影片播放相关view引用--end--
		}
	}

	/**
	 * onClick:點擊事件
	 * 
	 * @param v
	 * @see android.view.View.OnClickListener#onClick(android.view.View)
	 */
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		int id = v.getId();
		if (id > 100 && id < 101 + imageCount) {
			// 文章圖片按鈕切換
			SetHandoutScreen();
			/*
			 * Date dt = new Date(); if (lstClickTime != 0) { if (dt.getTime() -
			 * lstClickTime < 500) { SetHandoutScreen(); lstClickTime = 0; }
			 * else { lstClickTime = dt.getTime(); } } else { lstClickTime =
			 * dt.getTime(); }
			 */
		} else if (id > 0 && id < 100) {
			mScrollLayout.setToScreen(id - 1);
			setHandoutButtonBackGround(id);
		} else {
			// 其他按鈕
			switch (id) {
			case R.id.etTenderJoints:
				setDasTopMarginZero();
				break;
			case R.id.etSwollenJoints:
				setDasTopMarginZero();
				break;
			case R.id.etMgdl:
				setDasTopMarginZero();
				break;
			case R.id.etVAS:
				setDasTopMarginZero();
				break;
			case R.id.btnHealthEducateVideo:
				btn[0].setBackgroundResource(R.drawable.educator_video_over);
				btn[1].setBackgroundResource(R.drawable.educator_article_bt);
				btn[2].setBackgroundResource(R.drawable.das_bt);
				btn[3].setBackgroundResource(R.drawable.jianzheng_video_bt);
				btn[4].setBackgroundResource(R.drawable.survey_bt);
				DisplayFuncRect(0);
				initHVideoLayout();
				setHVideoList(1);
				break;
			case R.id.btnhControll:
				setHControll();
				break;
			case R.id.btnHandout:
				btn[0].setBackgroundResource(R.drawable.educator_video_bt);
				btn[1].setBackgroundResource(R.drawable.educator_article_over);
				btn[2].setBackgroundResource(R.drawable.das_bt);
				btn[3].setBackgroundResource(R.drawable.jianzheng_video_bt);
				btn[4].setBackgroundResource(R.drawable.survey_bt);
				DisplayFuncRect(1);
				setHandoutList();
				break;
			case R.id.btnDas:
				btn[0].setBackgroundResource(R.drawable.educator_video_bt);
				btn[1].setBackgroundResource(R.drawable.educator_article_bt);
				btn[2].setBackgroundResource(R.drawable.das_bt);
				btn[3].setBackgroundResource(R.drawable.jianzheng_video_bt);
				btn[4].setBackgroundResource(R.drawable.survey_bt);
				initDASLayout();
				DisplayFuncRect(2);
				break;
			case R.id.btnWitnessVideo:
				btn[0].setBackgroundResource(R.drawable.educator_video_bt);
				btn[1].setBackgroundResource(R.drawable.educator_article_bt);
				btn[2].setBackgroundResource(R.drawable.das_bt);
				btn[3].setBackgroundResource(R.drawable.jianzheng_video_over);
				btn[4].setBackgroundResource(R.drawable.survey_bt);
				DisplayFuncRect(3);
				initWVideoLayout();
				setWVideoList(2);
				break;
			case R.id.btnwControll:
				setWControll();
				break;
			case R.id.btnhScreen:
				SetHVideoScreen();
				break;
			case R.id.btnwScreen:
				SetWVideoScreen();
				break;
			case R.id.btnSurvey:
				btn[0].setBackgroundResource(R.drawable.educator_video_bt);
				btn[1].setBackgroundResource(R.drawable.educator_article_bt);
				btn[2].setBackgroundResource(R.drawable.das_bt);
				btn[3].setBackgroundResource(R.drawable.jianzheng_video_bt);
				btn[4].setBackgroundResource(R.drawable.survey_over);
				DisplayFuncRect(4);
				initSurveyLayout();
				setSurveyList();
				break;
			case R.id.btnSurveySend:
				SendSurvey();
				break;
			case R.id.btnPoint:
				if (et[1].isFocused()) {
					isBtnPointClicked = true;
				}
				if (isBtnPointClicked) {
					et[1].setText(et[1].getText() + ".");
					Editable editable = et[1].getText();
					Selection.setSelection(editable, editable.length());
					isBtnPointClicked = false;
				} else {
					Toast.makeText(mContext, "非小數值！", 1).show();
				}
				break;
			case R.id.btnRun:
				if (isCPR) {
					DAScalc("DAS28CPR");
				} else if (isESR) {
					DAScalc("DAS28ESR");
				}
				showDAS28.setText(strDAS28Val);
				showActivity.setText(strActivity);
				break;
			case R.id.btnClear:
				et[0].setText("");
				et[1].setText("");
				et[2].setText("");
				et[3].setText("");
				showDAS28.setText("");
				showActivity.setText("");
				strDAS28Val = "";
				strActivity = "";
				TJC28Val = 0.0;
				CPRMgdlVal = 0.0;
				SJC28Val = 0.0;
				VASVal = 0.0;
				DAS28Val = 0.0;
				break;
			case R.id.btnCPR:
				isCPR = true;
				isESR = false;
				showDAS28.setText("");
				showActivity.setText("");
				tvMgMm.setText("CRP (mg/L)");
				btnDAScalc[0].setBackgroundResource(R.drawable.cpr_over);
				btnDAScalc[1].setBackgroundResource(R.drawable.esr_normal);
				llDasRight.setBackgroundResource(R.drawable.dasright1_bg);
				break;
			case R.id.btnESR:
				isESR = true;
				isCPR = false;
				showDAS28.setText("");
				showActivity.setText("");
				tvMgMm.setText("ESR (mm/hr)");
				btnDAScalc[0].setBackgroundResource(R.drawable.cpr_normal);
				btnDAScalc[1].setBackgroundResource(R.drawable.esr_over);
				llDasRight.setBackgroundResource(R.drawable.dasright2_bg);
				break;
			case R.id.btnInputPreCode:
				Intent toDiseaseDialogIntent = new Intent(mContext,
						DiseaseDialog.class);
				startActivity(toDiseaseDialogIntent);
				break;
			case R.id.imgDiseaseLog:
				finish();
				break;
			}
		}
	}

	/**
	 * onLongClick:長按鍵事件
	 * 
	 * @param arg0
	 * @see android.view.View.OnLongClickListener#onLongClick(android.view.View)
	 */
	@Override
	public boolean onLongClick(View arg0) {
		// TODO Auto-generated method stub
		int iId = arg0.getId();
		if (iId > 100 && iId < 101 + imageCount) {
			SetHandoutScreen();
		} else if (iId == R.id.vvhMovies) {
			SetHVideoScreen();
		} else if (iId == R.id.vvwMovies) {
			SetWVideoScreen();
		}
		return false;
	}

	/**
	 * onItemClick:列表項點擊事件
	 * 
	 * @param arg0
	 *            :列表
	 * @param arg1
	 * @param arg2
	 *            :位置
	 * @param arg3
	 * @see android.widget.AdapterView.OnItemClickListener#onItemClick(android.widget.AdapterView,
	 *      android.view.View, int, long)
	 */
	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		// TODO Auto-generated method stub
		int id = arg0.getId();

		switch (id) {
		case R.id.lvHandout:
			laHandout.setSelectItem(arg2);
			mScrollLayout.setToScreen(0);
			String hID = lmHandout.get(arg2).get("ID").toString();
			setHandoutPic(hID);
			laHandout.notifyDataSetChanged(); // 该方法内部实现了在每个观察者上面调用onChanged事件。每当发现数据集有改变的情况，或者读取到数据的新状态时，就会调用此方法
			break;
		case R.id.lvhVideo:
			laHVideo.setSelectItem(arg2);
			laHVideo.notifyDataSetChanged();
			String vhID = lmHVideo.get(arg2).get("ID").toString();
			setHVideoPlayer(vhID);
			break;
		case R.id.lvwVideo:
			laWVideo.setSelectItem(arg2);
			laWVideo.notifyDataSetChanged();
			String vwID = lmWVideo.get(arg2).get("ID").toString();
			setWVideoPlayer(vwID);
			break;
		case R.id.lvSurvey:
			laSurvey.setSelectItem(arg2);
			String qnID = lmSurvey.get(arg2).get("qnID").toString();
			String surveyID = lmSurvey.get(arg2).get("ID").toString();
			setSurveyQuestions(new Long(surveyID).intValue(), new Long(qnID)
					.intValue());
			laSurvey.notifyDataSetChanged(); // 该方法内部实现了在每个观察者上面调用onChanged事件。每当发现数据集有改变的情况，或者读取到数据的新状态时，就会调用此方法
			break;
		}

	}

	/**
	 * DisplayFuncRect:顯示功能區域
	 * 
	 * @param _funcId
	 *            :功能區編號
	 */
	private void DisplayFuncRect(int _funcId) {

		vhVideo.pause();
		// rlhVideoView.setVisibility(View.GONE);
		llhController.setVisibility(View.GONE);
		vhVideo.setVisibility(View.GONE);
		ishPaused = true;
		btnhControll.setBackgroundResource(R.drawable.btnplay);
		vwVideo.pause();
		// rlwVideoView.setVisibility(View.GONE);
		llwController.setVisibility(View.GONE);
		vwVideo.setVisibility(View.GONE);
		iswPaused = true;
		btnwControll.setBackgroundResource(R.drawable.btnplay);
		if (_funcId < 2) {
			animationFromLeftToRightHideLayout(iCurrRl);
			animationFromLeftToRightShowLayout(_funcId);
		} else {
			animationFromRightToLeftHideLayout(iCurrRl);
			animationFromRightToLeftShowLayout(_funcId);
		}
		if (_funcId == 2) {
			// vTitle.setVisibility(View.GONE);
			setDasTopMarginZero();
			et[0].requestFocus();
		} else {
			// vTitle.setVisibility(View.VISIBLE);
		}
		// rl[iCurrRl].setVisibility(View.GONE);
		iCurrRl = _funcId;
	}

	/**
	 * animationFromLeftToRightHideLayout:動畫從左至右隱藏功能區域
	 * 
	 * @param _Id
	 *            :功能區編號
	 */
	private void animationFromLeftToRightHideLayout(int _Id) {
		animationTranslate = AnimationUtils.loadAnimation(this,
				R.anim.ltr_hide_alpha_translate);
		rl[_Id].startAnimation(animationTranslate);
		rl[_Id].setVisibility(View.GONE);
	}

	/**
	 * animationFromRightToLeftHideLayout:動畫從右至左隱藏功能區域
	 * 
	 * @param _Id
	 *            :功能區編號
	 */
	private void animationFromRightToLeftHideLayout(int _Id) {
		animationTranslate = AnimationUtils.loadAnimation(this,
				R.anim.rtl_hide_alpha_translate);
		rl[_Id].startAnimation(animationTranslate);
		rl[_Id].setVisibility(View.GONE);
	}

	/**
	 * animationFromLeftToRightShowLayout:動畫從左至右顯示功能區域
	 * 
	 * @param _Id
	 *            :功能區編號
	 */
	private void animationFromLeftToRightShowLayout(int _Id) {
		rl[_Id].setVisibility(View.VISIBLE);
		animationTranslate = AnimationUtils.loadAnimation(this,
				R.anim.ltr_show_alpha_translate);
		rl[_Id].startAnimation(animationTranslate);
	}

	/**
	 * animationFromRightToLeftShowLayout:動畫從右至左顯示功能區域
	 * 
	 * @param _Id
	 *            :功能區編號
	 */
	private void animationFromRightToLeftShowLayout(int _Id) {
		rl[_Id].setVisibility(View.VISIBLE);
		animationTranslate = AnimationUtils.loadAnimation(this,
				R.anim.rtl_show_alpha_translate);
		rl[_Id].startAnimation(animationTranslate);
	}

	// 衛教 视频相关功能代码--begin--
	/**
	 * setHVideoList:設置文教影片顯示
	 * 
	 * @param _Type
	 *            :影片類型
	 */
	private void setHVideoList(int _Type) {
		if (lmHVideo != null) {
			if (ishOnline) {
				llhController.setVisibility(View.VISIBLE);
				vhVideo.setVisibility(View.VISIBLE);
				rlhVideoView.setVisibility(View.VISIBLE);
			}
			return;
		}
		lmHVideo = dHelper.getVideoMapListByClassAndType(ClassID, _Type);
		if (lmHVideo.size() > 0) {
			laHVideo = new HandoutListAdapter(lmHVideo, this);
			lvhVideo.setAdapter(laHVideo);
			lvhVideo.setOnItemClickListener(this);
			laHVideo.setSelectItem(0);
			laHVideo.notifyDataSetChanged();
			String vID = lmHVideo.get(0).get("ID").toString();
			setHVideoPlayer(vID);
		} else {
			Toast.makeText(mContext, "目前暫無資料!", 1).show();
			llhController.setVisibility(View.GONE);
			vhVideo.setVisibility(View.GONE);
			rlhVideoView.setVisibility(View.GONE);
		}
	}

	/**
	 * setHVideoPlayer:顯示具體衛教影片
	 * 
	 * @param _vID
	 *            :影片編號
	 */
	private void setHVideoPlayer(String _vID) {
		pVideo video = dHelper.getVideoByID(_vID);
		tvhVTitle.setText(video.getName());
		tvhVDesp.setText(video.getDesc());
		ishOnline = false;
		vhVideo.stopPlayback();
		if (video.getLocalFile().trim().length() == 0) {
			llhController.setVisibility(View.GONE);
			vhVideo.setVisibility(View.GONE);
			rlhVideoView.setVisibility(View.GONE);
			return;
		}
		File f = new File(video.getLocalFile());
		if (f.exists()) {
			ishOnline = true;
			vhVideo.setVideoPath(video.getLocalFile());
			llhController.setVisibility(View.VISIBLE);
			vhVideo.setVisibility(View.VISIBLE);
			rlhVideoView.setVisibility(View.VISIBLE);
		} else {
			dHelper.clearVedioFile(new Long(_vID).longValue());
			llhController.setVisibility(View.GONE);
			vhVideo.setVisibility(View.GONE);
			rlhVideoView.setVisibility(View.GONE);
		}
	}

	/**
	 * SetHVideoScreen:設置衛教影片顯示屏幕
	 */
	private void SetHVideoScreen() {
		RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) rlhVideoView
				.getLayoutParams();// =rl.generateLayoutParams(null);
		if (ishVideoFullScreen) {
			// lp2=new LinearLayout.LayoutParams(480,640);
			lp.width = 528;
			lp.height = 512;
			lp.leftMargin = 338;
			lp.rightMargin = 416;
			lp.bottomMargin = 84;
			lp.topMargin = 156;
			lvhVideo.setVisibility(View.VISIBLE);
			btnhScreen.setBackgroundResource(R.drawable.btnscreen);
			setHVideoScale(SCREEN_DEFAULT);
		} else {
			lp.width = 1280;
			lp.height = 800 - 48;
			lp.leftMargin = 0;
			lp.rightMargin = 0;
			lp.bottomMargin = 0;
			lp.topMargin = 0;
			lvhVideo.setVisibility(View.GONE);
			btnhScreen.setBackgroundResource(R.drawable.btnfull);
			setHVideoScale(SCREEN_DEFAULT);
		}
		ishVideoFullScreen = !ishVideoFullScreen;
		rlhVideoView.setLayoutParams(lp);
	}

	/**
	 * setHControll:設置衛教影片的控制器
	 */
	private void setHControll() {
		if (ishPaused) {
			vhVideo.start();
			btnhControll.setBackgroundResource(R.drawable.btnpause);
		} else {
			vhVideo.pause();
			btnhControll.setBackgroundResource(R.drawable.btnplay);
		}
		ishPaused = !ishPaused;
	}

	/**
	 * setHVideoScale:設置衛教影片顯示大小
	 */
	private void setHVideoScale(int flag) {

		RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) rlhVideoView
				.getLayoutParams();
		switch (flag) {
		case SCREEN_FULL:
			vhVideo.setVideoScale(screenWidth, screenHeight);
			// getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
			break;
		case SCREEN_DEFAULT:
			int videoWidth = vhVideo.getVideoWidth();
			int videoHeight = vhVideo.getVideoHeight();

			int mWidth = lp.width;
			int mHeight = lp.height - 48;
			if (videoWidth > 0 && videoHeight > 0) {
				if (videoWidth * mHeight > mWidth * videoHeight) {
					mHeight = mWidth * videoHeight / videoWidth;
				} else if (videoWidth * mHeight < mWidth * videoHeight) {
					mWidth = mHeight * videoWidth / videoHeight;
				}
			}
			vhVideo.setVideoScale(mWidth, mHeight);
			break;
		}
	}

	// 衛教视频相关功能代码--end--

	// 文章模块相关代码--begin--
	Handout handout;

	/**
	 * setHandoutList:獲取文章顯示列表
	 */
	private void setHandoutList() {
		try {
			if (lmHandout != null) {
				return;
			}
			lmHandout = dHelper.getHandoutMapListByClass(pApplication
					.getPfizerDir(), ClassID);
			if (lmHandout.size() > 0) {
				laHandout = new HandoutListAdapter(lmHandout, this);
				laHandout.setSelectItem(0);
				lvHandout.setAdapter(laHandout);

				laHandout.notifyDataSetChanged();
				lvHandout.setOnItemClickListener(this);
				String hID = lmHandout.get(0).get("ID").toString();
				setHandoutPic(hID);
				rlPicView.setVisibility(View.VISIBLE);

			} else {
				Toast.makeText(mContext, "目前暫無資料!", 1).show();
				rlPicView.setVisibility(View.GONE);
			}
		} catch (Exception ex) {
			Toast.makeText(mContext, "加載衛教文章失敗：" + ex.getMessage(), 1).show();
			return;
		}
	}

	/**
	 * setHandoutPic:獲取某個文章的詳情
	 * 
	 * @param _hID
	 *            ：文章編號
	 */
	private void setHandoutPic(final String _hID) {
		/*
		 * new Thread(new Runnable() {
		 * 
		 * @Override public void run() {
		 */
		try {
			llPicConvert.removeAllViews();
			mScrollLayout.removeAllViews();
			handout = dHelper.getHandoutByID(_hID);
			imageCount = handout.getDlPicCnt();
			String imgPath = "";
			for (int i = 0; i < imageCount; i++) {
				llPicConvert.addView(addButtonView(i));
				imgPath = String.format("%sHandout/%d/%d-%d.%s", pApplication
						.getPfizerDir(), handout.getID(), handout.getID(),
						i + 1, handout.getFilenameExtension());
				mScrollLayout.addView(addImageView(i, imgPath));
			}
			if (imageCount > 0) {
				setHandoutButtonBackGround(0);
			}
			if (handout.getQuestionnaire() == -1) {
				btnTest.setVisibility(View.GONE);
			} else {
				btnTest.setVisibility(View.VISIBLE);
				btnTest.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						if (pApplication.getCaseCode().length() > 0) {
							if (dHelper.isHandtouTest(pApplication
									.getCaseCode(), handout.getQuestionnaire())) {
								Toast.makeText(mContext, "該病歷碼已經測驗，請重新輸入病歷碼!",
										1).show();
								return;
							}
						} else {
							Toast.makeText(mContext, "請輸入病歷碼後再進行測驗!", 1).show();
							return;
						}
						Intent intent = new Intent(DiseaseActivity.this,
								HandoutTestActivity.class);
						intent.putExtra("QNID", handout.getQuestionnaire());
						intent.putExtra("ClassID", ClassID);
						startActivity(intent);
					}
				});
			}
			llPicConvert.addView(addTextView(imageCount));
			setHandoutButtonBackGround(1);
		} catch (Exception ex) {
			// Toast.makeText(mContext, "加載衛教文章失敗：" + ex.getMessage(),
			// 1).show();
			return;
		}
		/*
		 * } }).start();
		 */
	}

	/**
	 * addTextView:設置文章頁碼
	 * 
	 * @param _PageCount
	 *            ：頁面數
	 */
	private View addTextView(int _PageCount) {
		TextView tv = new TextView(this);
		LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
				ViewGroup.LayoutParams.WRAP_CONTENT,
				ViewGroup.LayoutParams.WRAP_CONTENT);
		params.setMargins(0, 0, 20, 0);
		tv.setLayoutParams(params);
		tv.setTextSize(18);
		tv.setTextColor(Color.GRAY);
		tv.setText(String.format("（共%s頁）", _PageCount));
		return tv;
	}

	/**
	 * addButtonView:設置文章切換按鈕
	 * 
	 * @param id
	 *            ：編號
	 */
	private View addButtonView(int id) {
		Button btn = new Button(this);
		LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(32, 32);
		params.setMargins(0, 0, 12, 0);
		btn.setLayoutParams(params);
		btn.setPadding(0, 0, 0, 4);
		btn.setBackgroundResource(R.drawable.c_01);
		btn.setTextColor(Color.WHITE);
		btn.setTextSize(18);
		btn.setGravity(Gravity.CENTER);
		btn.setId(1 + id);
		btn.setText(String.valueOf(1 + id));
		btn.setOnClickListener(this);

		return btn;
	}

	/**
	 * addImageView:設置文章內容圖片
	 * 
	 * @param _ID
	 *            ：編號
	 * @param _URL
	 *            ：本地存放位置
	 */
	private View addImageView(int _ID, String _URL) {

		ImageView iv = new ImageView(this);
		LayoutParams params = new ViewGroup.LayoutParams(
				ViewGroup.LayoutParams.FILL_PARENT,
				ViewGroup.LayoutParams.FILL_PARENT);

		iv.setLayoutParams(params);
		iv.setPadding(0, 30, 0, 0);
		iv.setScaleType(ImageView.ScaleType.CENTER);
		iv.setId(101 + _ID);
		iv.setBackgroundDrawable(new BitmapDrawable(BitmapFactory.decodeFile(
				_URL, null)));
		iv.setOnClickListener(this);
		// iv.setOnLongClickListener(this);
		return iv;
	}

	/**
	 * setHandoutButtonBackGround:設置切換按鈕的背景圖片
	 * 
	 * @param id
	 *            ：編號
	 */
	private void setHandoutButtonBackGround(int id) {
		for (int i = 0; i < imageCount; i++) {
			Button btn = (Button) this.findViewById(i + 1);
			if (i + 1 == id) {
				btn.setBackgroundResource(R.drawable.c_01_over);
			} else {
				btn.setBackgroundResource(R.drawable.c_01);
			}
		}
	}

	/**
	 * SetHandoutScreen:設置文章內容圖片是否全屏顯示
	 */
	private void SetHandoutScreen() {
		RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) rlPicView
				.getLayoutParams();// =rl.generateLayoutParams(null);
		if (isHandoutFullScreen) {
			lp.height = 512;
			lp.width = 942;
			lp.bottomMargin = 84;
			lp.topMargin = 156;
			lvHandout.setVisibility(View.VISIBLE);
			llPicConvert.setVisibility(View.VISIBLE);
			btnPage.setVisibility(View.GONE);
			if (handout.getQuestionnaire() == -1) {
				btnTest.setVisibility(View.GONE);
			} else {
				btnTest.setVisibility(View.VISIBLE);
			}
		} else {
			lp.height = 800 - 48;
			lp.width = 1280;
			lp.bottomMargin = 0;
			lp.topMargin = 0;
			lvHandout.setVisibility(View.GONE);
			llPicConvert.setVisibility(View.GONE);
			btnPage.setText(String.format("第%d頁",
					mScrollLayout.getCurScreen() + 1));
			btnPage.setVisibility(View.VISIBLE);
			btnTest.setVisibility(View.GONE);
			task = new TimerTask() {
				@Override
				public void run() {
					// TODO Auto-generated method stub
					Message message = new Message();
					message.what = 1;
					handler.sendMessage(message);
				}
			};
			timer.schedule(task, 5000);
		}
		isHandoutFullScreen = !isHandoutFullScreen;
		rlPicView.setLayoutParams(lp);
	}

	// 文章功能模块--end--

	// 見證视频相关功能代码--begin--
	/**
	 * setHWideoList:設置見證影片顯示
	 * 
	 * @param _Type
	 *            :影片類型
	 */
	private void setWVideoList(int _Type) {
		if (lmWVideo != null) {
			if (iswOnline) {
				llwController.setVisibility(View.VISIBLE);
				vwVideo.setVisibility(View.VISIBLE);
				rlwVideoView.setVisibility(View.VISIBLE);
			}
			return;
		}
		lmWVideo = dHelper.getVideoMapListByClassAndType(ClassID, _Type);
		// vwVideo.set
		if (lmWVideo.size() > 0) {
			laWVideo = new HandoutListAdapter(lmWVideo, this);
			lvwVideo.setAdapter(laWVideo);
			lvwVideo.setOnItemClickListener(this);
			laWVideo.setSelectItem(0);
			laWVideo.notifyDataSetChanged();
			String vID = lmWVideo.get(0).get("ID").toString();
			setWVideoPlayer(vID);
		} else {
			Toast.makeText(mContext, "目前暫無資料!", 1).show();
			llwController.setVisibility(View.GONE);
			vwVideo.setVisibility(View.GONE);
			rlwVideoView.setVisibility(View.GONE);
		}
	}

	/**
	 * setWVideoPlayer:顯示具見證教影片
	 * 
	 * @param _vID
	 *            :影片編號
	 */
	private void setWVideoPlayer(String _vID) {
		pVideo video = dHelper.getVideoByID(_vID);
		tvwVTitle.setText(video.getName());
		tvwVDesp.setText(video.getDesc());
		iswOnline = false;
		vwVideo.stopPlayback();
		if (video.getLocalFile().trim().length() == 0) {
			llwController.setVisibility(View.GONE);
			vwVideo.setVisibility(View.GONE);
			rlwVideoView.setVisibility(View.GONE);
			return;
		}
		File f = new File(video.getLocalFile());
		if (f.exists()) {
			iswOnline = true;
			vwVideo.setVideoPath(video.getLocalFile());
			llwController.setVisibility(View.VISIBLE);
			vwVideo.setVisibility(View.VISIBLE);
			rlwVideoView.setVisibility(View.VISIBLE);
		} else {
			dHelper.clearVedioFile(new Long(_vID).longValue());
			llwController.setVisibility(View.GONE);
			vwVideo.setVisibility(View.GONE);
			rlwVideoView.setVisibility(View.GONE);
		}

	}

	private void SetWVideoScreen() {
		RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) rlwVideoView
				.getLayoutParams();// =rl.generateLayoutParams(null);
		if (iswVideoFullScreen) {
			// lp2=new LinearLayout.LayoutParams(480,640);
			lp.width = 528;
			lp.height = 512;
			lp.leftMargin = 338;
			lp.rightMargin = 416;
			lp.bottomMargin = 84;
			lp.topMargin = 156;
			lvwVideo.setVisibility(View.VISIBLE);
			btnwScreen.setBackgroundResource(R.drawable.btnscreen);
			setWVideoScale(SCREEN_DEFAULT);
		} else {

			lp.height = 800 - 48;
			lp.width = 1280;
			lp.leftMargin = 0;
			lp.rightMargin = 0;
			lp.bottomMargin = 0;
			lp.topMargin = 0;
			lvwVideo.setVisibility(View.GONE);
			btnwScreen.setBackgroundResource(R.drawable.btnfull);
			setWVideoScale(SCREEN_DEFAULT);
		}
		iswVideoFullScreen = !iswVideoFullScreen;
		rlwVideoView.setLayoutParams(lp);
	}

	private void setWControll() {
		if (iswPaused) {
			vwVideo.start();
			btnwControll.setBackgroundResource(R.drawable.btnpause);
		} else {
			vwVideo.pause();
			btnwControll.setBackgroundResource(R.drawable.btnplay);
		}
		iswPaused = !iswPaused;
	}

	/**
	 * SetWVideoScreen:設置見證影片顯示屏幕
	 */
	private void setWVideoScale(int flag) {

		RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) rlwVideoView
				.getLayoutParams();

		switch (flag) {
		case SCREEN_FULL:
			vwVideo.setVideoScale(screenWidth, screenHeight);
			getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
			break;
		case SCREEN_DEFAULT:
			int videoWidth = vwVideo.getVideoWidth();
			int videoHeight = vwVideo.getVideoHeight();
			int mWidth = lp.width;
			int mHeight = lp.height - 48;

			if (videoWidth > 0 && videoHeight > 0) {
				if (videoWidth * mHeight > mWidth * videoHeight) {
					mHeight = mWidth * videoHeight / videoWidth;
				} else if (videoWidth * mHeight < mWidth * videoHeight) {
					mWidth = mHeight * videoWidth / videoHeight;
				}
			}
			vwVideo.setVideoScale(mWidth, mHeight);
			getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
			break;
		}
	}

	// 衛教视频相关功能代码--end--

	// DAS计算模块--begin--
	private void DAScalc(String isWhat) {
		String strTJC = et[0].getText().toString();
		if (!strTJC.equals("")) {
			TJC28Val = Double.valueOf(strTJC);
		}

		String strCPRMgdl = et[1].getText().toString();
		if (!strCPRMgdl.equals("")) {
			CPRMgdlVal = Double.valueOf(strCPRMgdl);
		}

		String strSJC = et[2].getText().toString();
		if (!strSJC.equals("")) {
			SJC28Val = Double.valueOf(strSJC);
		}

		String strVAS = et[3].getText().toString();
		if (!strVAS.equals("")) {
			VASVal = Double.valueOf(strVAS);
		}

		if (TJC28Val < 0 || TJC28Val > 28) {
			Toast.makeText(mContext,
					"The Tender Joints input must  between 0 and 28!",
					Toast.LENGTH_SHORT).show();
			et[0].setText("");
			TJC28Val = 0.0;
			return;
		}
		if (isWhat.equals("DAS28CPR")) {
			if (CPRMgdlVal < 0 || CPRMgdlVal > 300) {
				Toast.makeText(mContext,
						"The CRP (mg/L) input must  between 0 and 300!",
						Toast.LENGTH_SHORT).show();
				et[1].setText("");
				CPRMgdlVal = 0.0;
				return;
			}
		} else {
			if (CPRMgdlVal < 1 || CPRMgdlVal > 300) {
				Toast.makeText(mContext,
						"The ESR (mm/hr) input must  between 1 and 300!",
						Toast.LENGTH_SHORT).show();
				et[1].setText("");
				CPRMgdlVal = 0.0;
				return;
			}
		}

		if (SJC28Val < 0 || SJC28Val > 28) {
			Toast.makeText(mContext,
					"The Swollen Joints input must  between 0 and 28!",
					Toast.LENGTH_SHORT).show();
			et[2].setText("");
			SJC28Val = 0.0;
			return;
		}

		if (VASVal < 0 || VASVal > 100) {
			Toast.makeText(mContext, "The VAS input must  between 0 and 100!",
					Toast.LENGTH_SHORT).show();
			et[3].setText("");
			VASVal = 0.0;
			return;
		}
		if (isWhat.equals("DAS28CPR")) {
			DAS28Val = 0.56 * (Math.sqrt(TJC28Val)) + 0.28
					* (Math.sqrt(SJC28Val)) + 0.36 * (Math.log(CPRMgdlVal + 1))
					+ 0.014 * VASVal + 0.96;
			// activity
			if (DAS28Val > 4.1) {
				strActivity = "high";
			} else if (DAS28Val > 2.7 && DAS28Val < 4.1) {
				strActivity = "moderate";
			} else if (DAS28Val < 2.7 && DAS28Val > 2.3) {
				strActivity = "low";
			} else if (DAS28Val < 2.3) {
				strActivity = "remission";
			}
			// 得到最后结果
			strDAS28Val = String.valueOf(round(DAS28Val, 2,
					BigDecimal.ROUND_HALF_UP));

		} else if (isWhat.equals("DAS28ESR")) {
			DAS28Val = 0.56 * (Math.sqrt(TJC28Val)) + 0.28
					* (Math.sqrt(SJC28Val)) + 0.7 * (Math.log(CPRMgdlVal))
					+ 0.014 * VASVal;
			// activity
			if (DAS28Val > 4.1) {
				strActivity = "high";
			} else if (DAS28Val > 2.7 && DAS28Val < 4.1) {
				strActivity = "moderate";
			} else if (DAS28Val < 2.7 && DAS28Val > 2.3) {
				strActivity = "low";
			} else if (DAS28Val < 2.3) {
				strActivity = "remission";
			}
			// 得到最后结果
			strDAS28Val = String.valueOf(round(DAS28Val, 2,
					BigDecimal.ROUND_HALF_UP));
		}

	}

	/***
	 * 控制精度
	 * 
	 * @param value
	 * @param scale
	 * @param roundingMode
	 * @return
	 */
	public static double round(double value, int scale, int roundingMode) {
		BigDecimal bd = new BigDecimal(value);
		bd = bd.setScale(scale, roundingMode);
		double d = bd.doubleValue();
		bd = null;
		return d;
	}

	// DAS计算模块--end--

	// 问卷模块相关代码--begin--
	/**
	 * setSurveyList:獲取問卷調查列表
	 */
	private void setSurveyList() {
		if (lmSurvey != null) {
			// rlqSurvey.setVisibility(View.VISIBLE);
			return;
		}
		lmSurvey = dHelper.getSurveyMapList();
		if (lmSurvey.size() > 0) {
			laSurvey = new SurveyListAdapter(lmSurvey, this);
			lvSurvey.setAdapter(laSurvey);
			lvSurvey.setOnItemClickListener(this);
			laSurvey.setSelectItem(0);
			laSurvey.notifyDataSetChanged();

			String qnID = lmSurvey.get(0).get("qnID").toString();
			String surveyID = lmSurvey.get(0).get("ID").toString();
			setSurveyQuestions(new Long(surveyID).intValue(), new Long(qnID)
					.intValue());
			rlqSurvey.setVisibility(View.VISIBLE);
		} else {
			Toast.makeText(mContext, "目前暫無資料!", 1).show();
			rlqSurvey.setVisibility(View.GONE);
		}
	}

	/**
	 * setSurveyQuestions:顯示某問卷調查詳情
	 * 
	 * @param _surveyID
	 *            :問卷調查編號
	 * @param _qnID
	 *            ：試題編號
	 */
	private void setSurveyQuestions(long _surveyID, long _qnID) {
		questionsList = dHelper.getQuestionList(_qnID);
		if (questionsList.size() > 0) {
			laQuestion = new QuestionListAdapter(questionsList, this,
					_surveyID, _qnID);
			laQuestion
					.setOnAnswerSelectedListener(new OnAnswerSelectedListener() {

						@Override
						public void OnAnswerSelected() {
							// TODO Auto-generated method stub
							tvAnswered.setText(String.format(
									"已測驗：%2d題\r\n共　有：%2d題", laQuestion
											.getUserSelectedCount(), laQuestion
											.getCount()));
						}
					});
			tvAnswered.setText(String.format("已測驗：%2d題\r\n共　有：%2d題", laQuestion
					.getUserSelectedCount(), laQuestion.getCount()));
			lvQuestions.setAdapter(laQuestion);
			laQuestion.notifyDataSetChanged();
		}
	}

	private void SendSurvey() {
		if (laQuestion != null) {
			try {
				if (laQuestion.getCount() > laQuestion.getUserSelectedCount()) {
					new AlertDialog.Builder(mContext).setTitle("信息提示")
							.setMessage("問卷調查中有未完成的選項，是否繼續提交？")
							.setPositiveButton("提交",
									new DialogInterface.OnClickListener() {
										@Override
										public void onClick(
												DialogInterface dialog,
												int which) {
											// TODO Auto-generated
											// method stub
											SendSurveyQuestion();
										}
									}).setNegativeButton("取消",
									new DialogInterface.OnClickListener() {
										@Override
										public void onClick(
												DialogInterface dialog,
												int which) {
											// TODO Auto-generated
											// method stub

										}
									}).show();
					return;
				}
				SendSurveyQuestion();
			} catch (Exception ex) {
				Toast.makeText(mContext, "發送數據失敗：" + ex.getMessage(),
						Toast.LENGTH_SHORT).show();
				return;
			}
		}
	}

	private void SendSurveyQuestion() {
		SurveyResult sResult = new SurveyResult();
		sResult.setDoctorName(etDoctorName.getText().toString());
		sResult.setOwnedHospitals(etHospital.getText().toString());
		sResult.setDivisions(etSubject.getText().toString());
		sResult.setSurveyID(laQuestion.getSurveyID());
		sResult.setQuestionNaire(laQuestion.getQnID());
		sResult.setSurveyResultCont(laQuestion.getUserSelectResult());
		Date dt = new Date();
		sResult.setSurveyResultTime(dt.getTime());
		sResult.setSurveyResultFlag(0);
		dHelper.addSurveyResult(sResult);
		Toast.makeText(mContext, "數據已經送出！", Toast.LENGTH_SHORT).show();
		etDoctorName.setText("");
		etHospital.setText("");
		etSubject.setText("");
		laQuestion.clearUserSelected();
		laQuestion.notifyDataSetChanged();
	}

	// 问卷模块相关代码--end--
	@Override
	// 监听退出键
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			if (isHandoutFullScreen) {
				SetHandoutScreen();
				return true;
			}
			if (ishVideoFullScreen) {
				SetHVideoScreen();
				return true;
			}
			if (iswVideoFullScreen) {
				SetWVideoScreen();
				return true;
			}
			finish();
		}
		return super.onKeyDown(keyCode, event);
	}

	public void onDestory() {
		super.onDestroy();
		mContext = null;
		dHelper = null;
		pApplication = null;

		rl = null;
		resTitle = null;
		resBottom = null;
		resVideoDesp = null;
		btn = null;
		et = null;

		btnInputPreCode = null;
		animationTranslate = null;
		log = null;
		// 全局變量設置---end--

		// 文章圖片預覽參數設置---begin--
		mScrollLayout = null;
		llPicConvert = null;
		rlHandout = null;
		rlPicView = null;
		llDasRight = null;
		tv = null;
		btnTest = null;
		lvHandout = null;
		lmHandout = null;
		laHandout = null;
		// 文章圖片預覽參數設置---end

		// 衛教影片模塊參數設置--begin--
		lmHVideo = null;
		laHVideo = null;
		vhVideo = null;
		btnhControll = null;
		btnhScreen = null;
		tvhVTime = null;
		tvhVTitle = null;
		tvhVDesp = null;
		lvhVideo = null;
		rlhVideoView = null;
		llhController = null;
		myhHandler = null;
		// 見證影片模塊參數設置--begin--
		vwVideo = null;
		btnwControll = null;
		btnwScreen = null;
		tvwVTime = null;
		tvwVTitle = null;
		tvwVDesp = null;
		lvwVideo = null;
		rlwVideoView = null;
		llwController = null;
		lmWVideo = null;
		laWVideo = null;
		mywHandler = null;

		etSubject = null;
		etHospital = null;
		etDoctorName = null;
		lvSurvey = null;
		lvQuestions = null;
		rlqSurvey = null;
		btnSurveySend = null;
		lmSurvey = null;
		laSurvey = null;
		questionsList = null;
		laQuestion = null;
		// 問卷模塊參數設置--end--

		// DAS計算參數設置---begin--
		showDAS28 = null;
		showActivity = null;
		btnDAScalc = null;
		System.gc();
	}

	@Override
	public boolean onKey(View v, int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if (keyCode == 4 && event.getAction() == KeyEvent.ACTION_UP
				&& iCurrRl == 2) {
			// vTitle.setVisibility(View.GONE);
			RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) rl[2]
					.getLayoutParams();
			lp.topMargin = 156;
			rl[2].setLayoutParams(lp);
			// sdf
		}
		if (keyCode == 66 && event.getAction() == KeyEvent.ACTION_UP) {
			int Id = v.getId();
			switch (Id) {
			case R.id.etTenderJoints:
				et[1].requestFocus();
				// et[1].setSelection(0, et[1].getText().toString().length());
				// et[1].setSelectAllOnFocus(true);
				break;
			case R.id.etMgdl:
				et[2].requestFocus();
				// et[2].setSelection(0, et[2].getText().toString().length());
				// et[2].setSelectAllOnFocus(true);
				break;
			case R.id.etSwollenJoints:
				et[3].requestFocus();
				// et[3].setSelection(0, et[3].getText().toString().length());
				// et[3].setSelectAllOnFocus(true);
				break;
			case R.id.etVAS:
				m.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
				if (isCPR) {
					DAScalc("DAS28CPR");
				} else if (isESR) {
					DAScalc("DAS28ESR");
				}
				showDAS28.setText(strDAS28Val);
				showActivity.setText(strActivity);
				// et[3].setInputType(InputType.TYPE_NULL);
				break;
			case R.id.etHospital:
				etSubject.requestFocus();
				// etSubject.setSelection(0,
				// etSubject.getText().toString().length());
				// etSubject.setSelectAllOnFocus(true);
				break;
			case R.id.etSubject:
				// etDoctorName.requestFocus();
				m.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
				break;
			case R.id.etDoctorName:
				etHospital.requestFocus();
				// etHospital.setSelection(0,
				// etHospital.getText().toString().length());
				// etHospital.setSelectAllOnFocus(true);
				// etDoctorName.setInputType(InputType.TYPE_NULL);
				break;
			default:
				break;
			}
			return true;
		} else if (keyCode == 66 && event.getAction() == KeyEvent.ACTION_DOWN) {
			return true;
		}
		return false;
	}

	@Override
	public void onFocusChange(View v, boolean hasFocus) {
		// TODO Auto-generated method stub
		int Id = v.getId();
		if (Id > 1000 & Id < 1005) {
			if (hasFocus) {
				setDasTopMarginZero();
			}
		}
		else
		{
			EditText _v=(EditText)v;
	        if (!hasFocus) {// 落空核心
	            _v.setHint(_v.getTag().toString());
	        } else {
	            String hint=_v.getHint().toString();
	            _v.setTag(hint);
	            _v.setHint("");
	        }
		}
	}

	private void setDasTopMarginZero() {
		SystemClock.sleep(100);
		RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) rl[2]
				.getLayoutParams();
		lp.topMargin = 0;
		rl[2].setLayoutParams(lp);
	}
}
